//
//  SYJHomeCell.swift
//  weibo
//
//  Created by 孙英建 on 16/4/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class SYJHomeCell: UITableViewCell {

    // 记录我们的底部toolBar 的约束
    var tabarViewTopConstraint: Constraint?
    
    // 接收控制器传入的数据
    var statusesModel: SYJStatusesModel?{
        didSet{
            originalView.statusModel = statusesModel
            
            // 卸载原有的底部视图约束
            self.tabarViewTopConstraint?.uninstall()
            
            // 判断是否有转发微博
            if statusesModel?.retweeted_status != nil{
                // 有
                retweetView.hidden = false
                
                retweetView.statusModel = statusesModel
                
                // 重新设置底部按钮的约束
                tabarView.snp_makeConstraints(closure: { (make) -> Void in
                    self.tabarViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            }else{
                // 没有
                retweetView.hidden = true
                
                // 重新设置底部按钮的约束
                tabarView.snp_makeConstraints(closure: { (make) -> Void in
                    self.tabarViewTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
            }
            
            tabarView.statusModel = statusesModel
        }
    }

    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置UI
    func setUI(){
        
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        // 添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(tabarView)
        contentView.addSubview(retweetView)
        
        
        // 添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        // 转发微博
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        // 底部按钮
        tabarView.snp_makeConstraints { (make) -> Void in
            self.tabarViewTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(37)
        }
        
        // 自身的约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(tabarView)
        }
        
    }
    
    
    
    // MARK: - 懒加载控件
    // 原创微博
    private lazy var originalView: SYJStatusOriginalView = {
       let orv = SYJStatusOriginalView()
        return orv
    }()
    
    // 底部转发评论赞
    lazy var tabarView: SYJToBarView = SYJToBarView()
    
    // 转发微博
    private lazy var retweetView: SYJRetweetView = SYJRetweetView()
    
}











